<?php
include_once CF_FUNCTIONS . "CF_support.php";


class CF_FilterQueryBasic{

	function CF_FilterQueryBasic($fieldspec){
		__construct($fieldspec);
	}
	
	function __construct($fieldspec){
		$this->fieldspec = $fieldspec;	
	}
	/**
	* @var string $selfpage References the page (usually $_PHP['SELF'])
	*/
	var $selfpage;
	/**
	* @var string $feildspec describes columns and option
	*/	
	var $fieldspec;
	/**
	* @var string $filterprefix prefix for filtered variables (column name used)
	*/
	var $filterprefix = 'flt_';	
	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 
	/**
	* @var bool $multiselect Determines whether to  implement multi-change logic on table
	*/
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	
    var $show_filter_submit = true;
	/**
	* @var string $show_filter_value string to show on filter button (default 'Apply')
	*/   	
	var $show_filter_value = 'Apply';
	/**
	* @var string $addmarkup string of markup to put in add form for other hidden fields, etc.
	*/  	
	function get_filterspec(){
		return $this->filters;
	}

	/**
	 * get_filter_vals() will recieve the filter variables from Session or request
     * and set them in the filters array.
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters are kept as filter [filter variable name][value]
     * Where filter variable name is filter prefix ($this->filterprefix) . variable name
	 */
	function get_filter_vals($method = 'req'){
		foreach($this->fieldspec AS $col_name => $colspec){
			$currentvalue = GetPersistent($this->filterprefix . $col_name,$method);
			if(!is_null($currentvalue)){
				$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
				$this->filters[$this->filterprefix . $col_name]['val'] = $currentvalue;
			}else{
				unset($this->filters[$this->filterprefix . $col_name]['var']);
				unset($this->filters[$this->filterprefix . $col_name]['val']);
			}
        }
		return $this->filters;
    }

	/**
	 * get_filter_pvals() will recieve the filter variables from Session or request
     * and set them in the filters array.  This uses the page specific filters storage
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters are kept as filter [filter variable name][value]
     * Where filter variable name is filter prefix ($this->filterprefix) . variable name
	 */	
	function get_filter_pvals($method = 'req'){
		foreach($this->fieldspec AS $col_name => $colspec){
			//if(isset($colspec['filter'])){
                $currentvalue = GetPagePersistent($this->filterprefix . $col_name,$method);
                if(!is_null($currentvalue)){
                    $this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
                    $this->filters[$this->filterprefix . $col_name]['val'] = $currentvalue;
                   // $this->filters[$this->filterprefix . $col_name]['source'] =
                }else{
					unset($this->filters[$this->filterprefix . $col_name]['var']);
					unset($this->filters[$this->filterprefix . $col_name]['val']);
				}
            //}
        }
		return $this->filters;
    }	
	
	//Set filter and Session value - generic	
	function set_filter_val($col_name, $filter_value, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
		$this->filters[$this->filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$this->filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - generic	
	function un_set_filter_val($col_name, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		unset($this->filters[$this->filterprefix . $col_name]);
		unset($_SESSION[$this->filterprefix . $col_name]);
	}
	//Set filter and Session value - page specific	
	function set_filter_pval($col_name, $filter_value, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		$this->filters[$this->filterprefix . $col_name]['var'] = $col_name;
		$this->filters[$this->filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$page_name]['filters'][$this->filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - page specific	
	function un_set_filter_pval($col_name, $page_name = ''){
		if($page_name == ''){
			$page_name = $this->selfpage;
		}
		unset($this->filters[$this->filterprefix . $col_name]);
		unset($_SESSION[$page_name]['filters'][$this->filterprefix . $col_name]);
	}

	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 * 
	 */
	function make_query_PSfilter(){

		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
	// CT 2009-10-27 Added ' AND (!empty($this->fieldspec[$currentvar]['filter']))
	// CT 2009-11-07 Added this as new if stmt to the correct line a few lines below
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					$flt_text .= " AND $tabledot" . $filter['var'] . " = :FLT" . $filter['var'] . " ";
				}
			}	
		}
	
	return $flt_text;
	}
	
	function make_PSfilter_bindings(&$stmt){
			if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
						$bindvar = ':FLT'. $filter['var'];
						$value = $filter['val'];
						$stmt->bindValue($bindvar,$value);
				}
			}	
		}
	}
	
	/**
	 * make_query_filter will create a query filter according to fieldspec settings, Request input, and Session data
	 * 
	 */
	function make_query_filter(){

		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
	// CT 2009-10-27 Added ' AND (!empty($this->fieldspec[$currentvar]['filter']))
	// CT 2009-11-07 Added this as new if stmt to the correct line a few lines below
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					$flt_text .= " AND $tabledot" . $filter['var'] . " = '" . $filter['val'] . "' ";
				}
			}	
		}
	
	return $flt_text;
	}
		
}


?>